package com.alei.redis.redisdemo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ShiLei
 * @version v1.0
 * @date 2020-06-27 19:34
 */
@RestController
public class RedisListDemoController {

    /**
     * 注入 redis 客户端连接池
     */
    @Resource
    private JedisPool jedisPool;

    /**
     * 生产者生产消息
     */
    @RequestMapping("testList")
    public Object testObject() {
        Jedis jedis = jedisPool.getResource();
        Long lpush = jedis.lpush("queue", "hello");
        return lpush;
    }

    /**
     * 消费者消费消息
     * 阻塞知道有消息或者超时  brpop  blpop
     * 缺点 只能供一个消费者消费
     */
    @RequestMapping("testListConsumer")
    public Object testListConsumer() {
        Jedis jedis = jedisPool.getResource();
        List<String> queue = jedis.brpop(300, "queue");
        return queue;
    }
}
